package com.cheung.leetcode.normal;

import java.util.HashMap;
import java.util.Map;

public class LC3 {

    public static int lengthOfLongestSubstring(String s) {
        int ans = 0, left = 0;
        Map<Character, Integer> hash = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            // 如果当前元素已经出现了，那么左边界就是字母上次出现时的位置加1
            if (hash.get(s.charAt(i)) != null) {
                left = Math.max(left, hash.get(s.charAt(i)) + 1);
            }
            hash.put(s.charAt(i), i);
            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abba"));
    }
}
